{% extends 'home.html' %}
{% block content %}

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>电位滴定仪</title>
    <link href="/static/css/kfc_yb.css" rel="stylesheet">
    </link>
    <script src="/static/bootstrap/js/jquery.min.js" type="text/javascript"></script>
    <script src="/static/js/dwy.js" type="text/javascript"></script>
</head>
<style>
    /* 保留其他样式不变 */
    table#tools input,
    table#tools select {
        width: 95% !important;
    }
</style>
</head>

<body>
    <form name="form1" method="post" id="form1">
        {% csrf_token %}
        <div class="c-layout-page">
            <div class="c-content-box c-size-md c-bg-white">
                <div class="container">
                    <div class="c-content-title-1">
                        <h3 class="c-center c-font-dark c-font-uppercase">电位滴定仪校准</h3>
                        <div class="c-line-center c-theme-bg"></div>
                        <p class="c-center"></p>
                    </div>

                    <!-- 仪器基础信息输入 -->
                    <div class="c-content-panel">
                        <div class="c-label">仪器基础信息输入</div>
                        <div class="c-body">
                            <div class="row">
                                <div class="col-md-12">
                                    <table class="table" id="tools">
                                        <tbody>
                                            <tr>
                                                <th scope="row" style="text-align:center">仪器名称</th>
                                                <td colspan="2"><input name="A1PH_Name01" type="text" value="电位滴定仪"
                                                        id="A1PH_Name01" style="height:25px;width:350px"></td>
                                                <th style="text-align:center">仪器接收编号</th>
                                                <td colspan="2"><input name="A1PH_Received02" type="text"
                                                        value="{{ A1PH_Received02 }}" readonly="readonly"
                                                        id="A1PH_Received02"
                                                        style="background-color:#F3F3F3;height:25px; width:300px">
                                                </td>
                                            </tr>

                                            <tr>
                                                <th scope="row" style="text-align:center">型号/规格</th>
                                                <td><input name="A1PH_ProdModel03" type="text" id="A1PH_ProdModel03"
                                                        style="height:25px;width:240px"></td>
                                                <th style="text-align:center">仪器序列号</th>
                                                <td><input name="A1PH_SN04" type="text" id="A1PH_SN04"
                                                        style="height:25px">
                                                </td>
                                                <!-- ... existing code ... -->
                                                <th style="text-align:center">环境条件</th>
                                                <td>
                                                    <input name="A1PH_Wd11" type="text" id="A1PH_Wd11"
                                                        style="height:25px; width:50px !important;"
                                                        onblur="formatToOneDecimal(this)">&nbsp;°C &nbsp;&nbsp;
                                                    <input name="A1PH_Sd12" type="text" id="A1PH_Sd12"
                                                        style="height:25px; width:50px !important;"
                                                        onblur="formatToOneDecimal(this)">&nbsp;%RH
                                                    <script>
                                                        function formatToOneDecimal(inputElement) {
                                                            if (inputElement.value !== '') {
                                                                const value = parseFloat(inputElement.value);
                                                                if (!isNaN(value)) {
                                                                    inputElement.value = value.toFixed(1);
                                                                }
                                                            }
                                                        }

                                                        // 页面加载时也格式化已有的值
                                                        document.addEventListener('DOMContentLoaded', function () {
                                                            formatToOneDecimal(document.getElementById('A1PH_Wd11'));
                                                            formatToOneDecimal(document.getElementById('A1PH_Sd12'));
                                                        });
                                                    </script>
                                                </td>
                                                <!-- ... existing code ... -->
                                            </tr>
                                            <tr>
                                                <th style="text-align:center" scope="row">校准依据</th>
                                                <td colspan="3"><input name="A1PH_Xz06" type="text"
                                                        value="JJG 658-2022电位滴定仪检定规程" id="A1PH_Xz06"
                                                        style="height:25px;width:420px"></td>
                                                <th style="text-align:center">校准日期</th>
                                                <td>
                                                    <input name="A1PH_Rq07" type="text" value="{{ date }}"
                                                        readonly="readonly" id="A1PH_Rq07"
                                                        style="background-color: #f3f3f3; height: 25px; width: 120px" />
                                                </td>
                                            </tr>

                                            <tr>
                                                <td colspan="6"
                                                    style="height:20px;text-align:center;background-color:aliceblue">
                                                    标准器及配套设备
                                                </td>
                                            </tr>

                                            <tr>
                                                <td colspan="6">
                                                    <table class="table" id="tools">
                                                        <tbody>
                                                            <tr>

                                                                <th style="text-align:center">名称/型号</th>
                                                                <th style="text-align:center">编号</th>
                                                                <th style="text-align:center">测量范围</th>
                                                                <th style="text-align:center">批号/证书编号</th>
                                                                <th style="text-align:center">有效期</th>
                                                                <th style="text-align:center">校准机构</th>
                                                                <th style="text-align:center">计量特性</th>
                                                            </tr>
                                                            <tr>
                                                                <td><input name="BLPH_Xmxh16" type="text"
                                                                        id="BLPH_Xmxh16" value="/" style="height:25px">
                                                                </td>
                                                                <td><select name="BLPH_Bh17" id="BLPH_Bh17"
                                                                        style="height:30px;width:107px;">
                                                                        <option selected="selected" value="0">==请选择==
                                                                        </option>
                                                                        {% for tool in tools %}
                                                                        <option value="{{ tool.tool_number }}">
                                                                            {{tool.tool_number }}</option>
                                                                        {% endfor %}

                                                                    </select></td>
                                                                <td><input name="BLPH_FW_01" type="text" id="BLPH_FW_01"
                                                                        value="/" style="height:25px;width:100px"></td>
                                                                <td><input name="BLPH_Jlzsbh18" type="text" value="/"
                                                                        id="BLPH_Jlzsbh18" style="height:25px"></td>
                                                                <td><input name="BLPH_Yxrq19" type="text"
                                                                        id="BLPH_Yxrq19" value="/" style="height:25px">
                                                                </td>
                                                                <td><input name="BLPH_Yzjg20" type="text"
                                                                        id="BLPH_Yzjg20" value="/"
                                                                        style="height:25px;width:100px"></td>
                                                                <td><input name="BLPH_Zqddj21" type="text"
                                                                        id="BLPH_Zqddj21" value="/"
                                                                        style="height:25px; width:100px"></td>

                                                            </tr>
                                                            <tr>
                                                                <td><input name="BLPH_Xmxh22" type="text" value="/"
                                                                        id="BLPH_Xmxh22" style="height:25px"></td>
                                                                <td><select name="BLPH_Bh23" id="BLPH_Bh23"
                                                                        style="height:30px;width:107px;">
                                                                        <option selected="selected" value="0">==请选择==
                                                                        </option>
                                                                        {% for tool in tools %}
                                                                        <option value="{{ tool.tool_number }}">{{tool.tool_number }}</option>
                                                                        {% endfor %}

                                                                    </select></td>
                                                                <td><input name="BLPH_FW_02" type="text" value="/"
                                                                        id="BLPH_FW_02" style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Jlzsbh24" type="text" value="/"
                                                                        id="BLPH_Jlzsbh24" style="height:25px"></td>
                                                                <td><input name="BLPH_Yxrq25" type="text" value="/"
                                                                        id="BLPH_Yxrq25" style="height:25px"></td>
                                                                <td><input name="BLPH_Yzjg26" type="text" value="/"
                                                                        id="BLPH_Yzjg26"
                                                                        style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Zqddj27" type="text" value="/"
                                                                        id="BLPH_Zqddj27"
                                                                        style="width:100px;height:25px">
                                                                </td>

                                                            </tr>
                                                            <tr>
                                                                <td><input name="BLPH_Xmxh28" type="text" value="/"
                                                                        id="BLPH_Xmxh28" style="height:25px"></td>
                                                                <td><select name="BLPH_Bh29" id="BLPH_Bh29" 
                                                                      style="height:30px;width:107px;">
                                                                        <option selected="selected" value="0">==请选择==
                                                                        </option>
                                                                        {% for tool in tools %}
                                                                        <option value="{{ tool.tool_number }}">{{tool.tool_number }}</option>
                                                                        {% endfor %}

                                                                    </select></td>

                                                                <td><input name="BLPH_FW_03" type="text" value="/"
                                                                        id="BLPH_FW_03" style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Jlzsbh30" type="text" value="/"
                                                                        id="BLPH_Jlzsbh30" style="height:25px"></td>
                                                                <td><input name="BLPH_Yxrq31" type="text" value="/"
                                                                        id="BLPH_Yxrq31" style="height:25px"></td>
                                                                <td><input name="BLPH_Yzjg32" type="text" value="/"
                                                                        id="BLPH_Yzjg32"
                                                                        style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Zqddj33" type="text" value="/"
                                                                        id="BLPH_Zqddj33"
                                                                        style="width:100px;height:25px">
                                                                </td>

                                                            </tr>
                                                            <tr>
                                                                <td><input name="BLPH_Xmxh34" type="text" value="/"
                                                                        id="BLPH_Xmxh34" style="height:25px"></td>
                                                                <td><input name="BLPH_Bh35" id="BLPH_Bh35" value="/"
                                                                        style="height:30px;width:107px;">

                                                                <td><input name="BLPH_FW_04" type="text" value="/"
                                                                        id="BLPH_FW_04" style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Jlzsbh36" type="text" value="/"
                                                                        id="BLPH_Jlzsbh36" style="height:25px"></td>
                                                                <td><input name="BLPH_Yxrq37" type="text" value="/"
                                                                        id="BLPH_Yxrq37" style="height:25px"></td>
                                                                <td><input name="BLPH_Yzjg38" type="text" value="/"
                                                                        id="BLPH_Yzjg38"
                                                                        style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Zqddj39" type="text" value="/"
                                                                        id="BLPH_Zqddj39"
                                                                        style="width:100px;height:25px">
                                                                </td>

                                                            </tr>
                                                            <tr>
                                                                <td><input name="BLPH_Xmxh40" type="text" value="/"
                                                                        id="BLPH_Xmxh40" style="height:25px"></td>
                                                                <td><input name="BLPH_Bh41" id="BLPH_Bh41" value="/"
                                                                        style="height:30px;width:107px;">


                                                                    </select></td>
                                                                <td><input name="BLPH_FW_05" type="text" value="/"
                                                                        id="BLPH_FW_05" style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Jlzsbh42" type="text" value="/"
                                                                        id="BLPH_Jlzsbh42" style="height:25px"></td>
                                                                <td><input name="BLPH_Yxrq43" type="text" value="/"
                                                                        id="BLPH_Yxrq43" style="height:25px"></td>
                                                                <td><input name="BLPH_Yzjg44" type="text" value="/"
                                                                        id="BLPH_Yzjg44"
                                                                        style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Zqddj45" type="text" value="/"
                                                                        id="BLPH_Zqddj45"
                                                                        style="width:100px;height:25px">
                                                                </td>

                                                            </tr>
                                                            <tr>
                                                                <td><input name="BLPH_Xmxh46" type="text" value="/"
                                                                        id="BLPH_Xmxh46" style="height:25px"></td>
                                                                <td><input name="BLPH_Bh47" id="BLPH_Bh47" value="/"
                                                                        style="height:30px;width:107px;">

                                                                <td><input name="BLPH_FW_06" type="text" value="/"
                                                                        id="BLPH_FW_06" style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Jlzsbh48" type="text" value="/"
                                                                        id="BLPH_Jlzsbh48" style="height:25px"></td>
                                                                <td><input name="BLPH_Yxrq49" type="text" value="/"
                                                                        id="BLPH_Yxrq49" style="height:25px"></td>
                                                                <td><input name="BLPH_Yzjg50" type="text" value="/"
                                                                        id="BLPH_Yzjg50"
                                                                        style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Zqddj51" type="text" value="/"
                                                                        id="BLPH_Zqddj51"
                                                                        style="width:100px;height:25px">
                                                                </td>

                                                            </tr>
                                                            <tr>
                                                                <td>生产厂商</td>
                                                                <td colspan="6"><input name="A1PH_Sccs52" type="text"
                                                                        value="梅特勒托利多" id="A1PH_Sccs52"
                                                                        style="width:900px;height:25px"></td>
                                                            </tr>
                                                            <tr>
                                                                <td>证书单位</td>
                                                                <td colspan="6"><input name="A1PH_Zsdw53" type="text"
                                                                        id="A1PH_Zsdw53"
                                                                        style="width:900px;height:25px"></td>
                                                            </tr>
                                                            <tr>
                                                                <td>证书地址</td>
                                                                <td colspan="6"><input name="A1PH_Zsdz54" type="text"
                                                                        id="A1PH_Zsdz54"
                                                                        style="width:900px;height:25px"></td>
                                                            </tr>
                                                            <tr>
                                                                <td>校准地点</td>
                                                                <td colspan="6"><input name="A1PH_Xzdd55" type="text"
                                                                        id="A1PH_Xzdd55"
                                                                        style="width:900px;height:25px"></td>
                                                            </tr>
                                                        </tbody>
                                                    </table>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 电计性能及仪器示值部分 -->
                    <div class="c-content-panel">
                        <div class="c-label">一、电计性能及仪器示值</div>
                        <div class="c-body" id="sensor-container">
                        </div>

                        <!-- 滴定管容量误差部分 -->
                        <div class="c-content-panel">
                            <div class="c-label">二、滴定管</div>
                            <div class="c-body" id="ddg-container">
                                <!-- 滴定管部分将在这里动态加载 -->
                            </div>
                        </div>

                        <!-- 本次校准结果的扩展不确定度 -->
                        <div class="c-content-panel">
                            <div class="c-label">本次校准结果的扩展不确定度 (<i>k</i>=2)</div>
                            <div class="c-body">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="table-responsive">
                                            <table class="table">
                                                <tbody>
                                                    <tr>
                                                        <th>电计(mV):<i>U</i>=</th>
                                                        <th>滴定管容量误差(mL):<i>U</i>=</th>
                                                        <th>仪器示值误差(%):<i>U</i>=</th>
                                                    </tr>
                                                    <tr>
                                                        <td><input name="K0" type="text" id="K0"
                                                                style="width:110px; height:25px"></td>
                                                        <td><input name="K1" type="text" id="K1"
                                                                style="width:110px; height:25px"></td>
                                                        <td><input name="K2" type="text" id="K2"
                                                                style="width:110px; height:25px"></td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                        <div class="table-responsive">
                                            <table class="table table-bordered">
                                                <tbody>
                                                    <tr>
                                                        <th>校准员：</th>
                                                        <td><input name="calibrator" type="text" id="calibrator"
                                                                style="width:110px; height:25px;border-width: 0.5px; border-style: inset; background-color: #F3F3F3"
                                                                value="{{ windows_user }}"></td>
                                                        <th>核验员</th>
                                                        <td><input name="inspector_Users" type="text"
                                                                id="inspector_Users" style="width:110px; height:25px">
                                                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                            <!-- 将原来第646行左右的提交按钮代码替换为： -->
                                            <div class="S001">
                                                <br>
                                                <table>
                                                    <tbody>
                                                        <tr>
                                                            <td colspan="5" align="center">
                                                                <input type="submit" name="Button2" value="提交校准"
                                                                    onclick="javascript:if(Button2_onclick()==false) return false;"
                                                                    id="Button2" style="width:80px;">
                                                            </td>

                                                        </tr>

                                                        <tr>
                                                            <td colspan="5" align="center">
                                                                <a href="/add/" class="btn btn-default btn-xs"
                                                                    style="margin-top: 10px;width: 80px;">取 消</a>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
    </form>

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-body">
                <div class="alert alert-danger alert-dismissible fade in" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                    <h4>注意!</h4>
                    <p>请确认相关数据已经填写正确，提交后无法修改！！！</p>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                        </button>
                        <button type="button" class="btn btn-danger" onclick="confirmAdd();">
                            确定提交
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
{% endblock %}

{% block js %}

<script>

    function collectSensorData() {
        const sensors = [];
        $('.sensor-content').each(function () {
            const sensorIndex = $(this).attr('id').split('-')[2]; // 从id中提取索引
            const sensor = {};

            $(this).find('input[data-original-name]').each(function () {
                const originalName = $(this).attr('data-original-name');
                const value = $(this).val();
                sensor[originalName] = value;
            });

            $(this).find('select[data-original-name]').each(function () {
                const originalName = $(this).attr('data-original-name');
                const value = $(this).val();
                sensor[originalName] = value;
            });

            sensors.push(sensor);
        });

        return sensors;
    }

    function collectDdgData() {
        const ddgs = [];
        $('.ddg-content').each(function () {
            const ddgIndex = $(this).attr('id').split('-')[2]; // 从id中提取索引
            const ddg = {};

            $(this).find('input[data-original-name]').each(function () {
                const originalName = $(this).attr('data-original-name');
                const value = $(this).val();
                ddg[originalName] = value;
            });

            $(this).find('select[data-original-name]').each(function () {
                const originalName = $(this).attr('data-original-name');
                const value = $(this).val();
                ddg[originalName] = value;
            });

            ddgs.push(ddg);
        });

        return ddgs;
    }

    $(document).ready(function () {

         $('#tools tr:gt(0)').each(function () {
            $(this).find('td:eq(1) select').change(function () {
                var selectedValue = $(this).val();
                var title = $(this).parent().siblings().eq(0).find('input').first()
                // var range = $(this).parent().siblings().eq(1).find('input').first()
                // var certificate_number = $(this).parent().siblings().eq(2).find('input').first()
                // var end_date = $(this).parent().siblings().eq(3).find('input').first()
                var cal_jigou = $(this).parent().siblings().eq(4).find('input').first()
                var property = $(this).parent().siblings().eq(5).find('input').first()

                $.ajax({
                    type: "GET",
                    url: "/tools/",  // 替换为您的URL
                    dataType: 'JSON',
                    data: {
                        'value': selectedValue,
                    },
                    success: function (data) {
                        // 假设返回的数据填充到input
                        var n = data.datalist;
                        title.val(n[0].fields.title);
                        // range.val(n[0].fields.range);
                        // certificate_number.val(n[0].fields.certificate_number);
                        // end_date.val(n[0].fields.end_date);
                        cal_jigou.val(n[0].fields.cal_jigou);
                        property.val(n[0].fields.property);
                    }
                });
            });

        });


        // 从URL参数获取电极和滴定管数量
        const urlParams = new URLSearchParams(window.location.search);
        const sensorCount = parseInt(urlParams.get('sensor')) || 1;
        const ddgCount = parseInt(urlParams.get('ddg')) || 1;

        // 加载电极部分
        loadSensors(sensorCount);

        // 加载滴定管部分
        loadDdgs(ddgCount);

        // 加载电极模板
        function loadSensors(count) {
            const container = $('#sensor-container');
            container.empty();

            for (let i = 1; i <= count; i++) {
                const panel = $(`
                    <div class="sensor-panel" id="sensor-panel-${i}">
                        <div class="c-content-panel">
                            <div class="c-label">${i}、电极接口 SENSOR${i}</div>
                            <div class="c-body sensor-content" id="sensor-content-${i}">
                                <!-- 电极内容将通过ajax加载 -->
                            </div>
                        </div>
                    </div>
                `);
                container.append(panel);

                // 异步加载sensor内容
                loadSensorContent(i);
            }
        }

        // 加载单个电极内容
        // 修改传感器加载函数，自动处理所有函数调用中的传感器索引
        function loadSensorContent(sensorIndex) {
            $.ajax({
                url: '/static/temp/sensor.html',
                type: 'GET',
                success: function (data) {
                    // 创建一个临时容器来处理HTML
                    const tempDiv = $('<div>').html(data);

                    // 定义需要添加传感器索引的函数列表
                    const functionNames = [
                        '_SetDigitalInput',
                        '__SetDigitalInput',
                        '___SetDigitalInput',
                        'SetDigitalInputC',
                        'SetDigitalInputD',
                        'SetDigitalInputF'
                    ];

                    // 修改input元素
                    tempDiv.find('input').each(function () {
                        const originalName = $(this).attr('name');
                        const originalId = $(this).attr('id');
                        if (originalName) {
                            // 保持name不变，只在id后添加索引
                            const newId = originalId ? originalId + '_sensor' + sensorIndex : originalName + '_sensor' + sensorIndex;
                            $(this).attr('id', newId);
                            $(this).attr('data-sensor-index', sensorIndex);
                            $(this).attr('data-original-name', originalName);

                            // 动态更新onchange中的函数调用参数
                            const onchange = $(this).attr('onchange');
                            if (onchange) {
                                let newOnchange = onchange;
                                // 遍历所有需要处理的函数
                                functionNames.forEach(funcName => {
                                    // 使用正则表达式匹配函数调用并添加传感器索引参数
                                    const regex = new RegExp(`${funcName}\\s*\\(\\s*(\\d+)\\s*\\)`, 'g');
                                    newOnchange = newOnchange.replace(regex, (match, param) => {
                                        return `${funcName}(${param}, ${sensorIndex})`;
                                    });
                                });
                                $(this).attr('onchange', newOnchange);
                            }
                        }
                    });

                    // 修改select元素（逻辑相同）
                    tempDiv.find('select').each(function () {
                        const originalName = $(this).attr('name');
                        const originalId = $(this).attr('id');
                        if (originalName) {
                            // 保持name不变，只在id后添加索引
                            const newId = originalId ? originalId + '_sensor' + sensorIndex : originalName + '_sensor' + sensorIndex;
                            $(this).attr('id', newId);
                            $(this).attr('data-sensor-index', sensorIndex);
                            $(this).attr('data-original-name', originalName);

                            // 处理onchange事件
                            const onchange = $(this).attr('onchange');
                            if (onchange) {
                                let newOnchange = onchange;
                                functionNames.forEach(funcName => {
                                    const regex = new RegExp(`${funcName}\\s*\\(\\s*(\\d+)\\s*\\)`, 'g');
                                    newOnchange = newOnchange.replace(regex, (match, param) => {
                                        if (funcName === '_SetDigitalInput') {
                                            return `${funcName}(${param}, ${sensorIndex})`;
                                        } else {
                                            return `${funcName}(${param}, ${sensorIndex})`;
                                        }
                                    });
                                });

                                $(this).attr('onchange', newOnchange);
                            }
                        }
                    });

                    // 将处理后的内容放入对应的容器
                    $(`#sensor-content-${sensorIndex}`).html(tempDiv.html());
                },
                error: function (xhr, status, error) {
                    $(`#sensor-content-${sensorIndex}`).html('<p>加载电极模板失败</p>');
                }
            });
        }

        // 加载滴定管模板
        function loadDdgs(count) {
            const container = $('#ddg-container');
            container.empty();

            for (let i = 1; i <= count; i++) {
                const panel = $(`
                    <div class="ddg-panel" id="ddg-panel-${i}">
                        <div class="c-content-panel">
                            <div class="c-label">${i}、滴定管</div>
                            <div class="c-body ddg-content" id="ddg-content-${i}">
                                <!-- 滴定管内容将通过ajax加载 -->
                            </div>
                        </div>
                    </div>
                `);
                container.append(panel);

                // 异步加载ddg内容
                loadDdgContent(i);
            }
        }

        // 修改滴定管加载函数，自动处理所有函数调用中的滴定管索引
        function loadDdgContent(ddgIndex) {
            $.ajax({
                url: '/static/temp/ddg.html',
                type: 'GET',
                success: function (data) {
                    // 创建一个临时容器来处理HTML
                    const tempDiv = $('<div>').html(data);

                    // 定义需要添加滴定管索引的函数列表
                    const functionNames = [
                        'searchChange',
                        'searchChangeE0',
                        'SetDigitalInputE'
                    ];

                    // 修改input元素
                    tempDiv.find('input').each(function () {
                        const originalName = $(this).attr('name');
                        const originalId = $(this).attr('id');
                        if (originalName) {
                            // 保持name不变，只在id后添加索引
                            const newId = originalId ? originalId + '_ddg' + ddgIndex : originalName + '_ddg' + ddgIndex;
                            $(this).attr('id', newId);
                            $(this).attr('data-ddg-index', ddgIndex);
                            $(this).attr('data-original-name', originalName);

                            // 动态更新onchange中的函数调用参数
                            const onchange = $(this).attr('onchange');
                            if (onchange) {
                                let newOnchange = onchange;
                                // 遍历所有需要处理的函数
                                functionNames.forEach(funcName => {
                                    // 使用正则表达式匹配函数调用并添加滴定管索引参数
                                    const regex = new RegExp(`${funcName}\\s*\\(\\s*(\\d*)\\s*\\)`, 'g');
                                    newOnchange = newOnchange.replace(regex, (match, param) => {
                                        // 如果参数为空或者是searchChange函数，则传递索引
                                        if (funcName === 'searchChange') {
                                            // searchChange可以不传递参数，函数内部会自动处理
                                            return `${funcName}()`;
                                        } else {
                                            const paramValue = param ? param : '0';
                                            return `${funcName}(${paramValue}, ${ddgIndex})`;
                                        }
                                    });
                                });
                                $(this).attr('onchange', newOnchange);
                            }
                        }
                    });

                    // 修改select元素（逻辑相同）
                    tempDiv.find('select').each(function () {
                        const originalName = $(this).attr('name');
                        const originalId = $(this).attr('id');
                        if (originalName) {
                            // 保持name不变，只在id后添加索引
                            const newId = originalId ? originalId + '_ddg' + ddgIndex : originalName + '_ddg' + ddgIndex;
                            $(this).attr('id', newId);
                            $(this).attr('data-ddg-index', ddgIndex);
                            $(this).attr('data-original-name', originalName);

                            // 处理onchange事件
                            const onchange = $(this).attr('onchange');
                            if (onchange) {
                                let newOnchange = onchange;
                                functionNames.forEach(funcName => {
                                    const regex = new RegExp(`${funcName}\\s*\\(\\s*(\\d*)\\s*\\)`, 'g');
                                    newOnchange = newOnchange.replace(regex, (match, param) => {
                                        // 如果参数为空或者是searchChange函数，则传递索引
                                        if (funcName === 'searchChange') {
                                            // searchChange可以不传递参数，函数内部会自动处理
                                            return `${funcName}()`;
                                        } else {
                                            const paramValue = param ? param : '0';
                                            return `${funcName}(${paramValue}, ${ddgIndex})`;
                                        }
                                    });
                                });

                                $(this).attr('onchange', newOnchange);
                            }
                        }
                    });

                    // 将处理后的内容放入对应的容器
                    $(`#ddg-content-${ddgIndex}`).html(tempDiv.html());
                },
                error: function (xhr, status, error) {
                    $(`#ddg-content-${ddgIndex}`).html('<p>加载滴定管模板失败</p>');
                }
            });
        }

        // 添加隐藏字段来存储传感器和滴定管数据
        $('#form1').append('<input type="hidden" name="sensor_data" id="sensor_data">');
        $('#form1').append('<input type="hidden" name="ddg_data" id="ddg_data">');
        
        // 在表单提交前填充这些隐藏字段
        $('#form1').submit(function() {
            const sensorData = collectSensorData();
            const ddgData = collectDdgData();
            
            $('#sensor_data').val(JSON.stringify(sensorData));
            $('#ddg_data').val(JSON.stringify(ddgData));
        });
    });

</script>
{% endblock %}